import {createApp} from 'vue';
import App from './App.vue';
import ElementPlus, {ElMessage} from 'element-plus';
import 'element-plus/dist/index.css';
import router from "@/router/index.ts";
import axios from "axios";
import Particles from 'particles.vue3';
import "./input.css";
import 'vue3-slide-verify/dist/style.css';
import 'element-plus/theme-chalk/dark/css-vars.css';

//防止sockjs报错
if (typeof window !== 'undefined') {
    window.global = window;
}

const app = createApp(App);
app.use(ElementPlus);
app.use(router)
app.use(Particles);
// 注释掉这两个，浏览器就不会报警了，不注释掉也没有影响
// app.use(SlideVerify);
// app.use(Notifications)

app.config.globalProperties.$axios = axios;
axios.defaults.baseURL = "http://121.199.30.8:9937"
// 提供全局变量
app.provide('uploadUrl', "http://121.199.30.8:9937/common/upload");
app.provide('inshow', true)

// axios请求拦截器
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
});


// 响应拦截器
axios.interceptors.response.use(response => {
    return response;
}, error => {
    const originalRequest = error.config;
    if (error.response && (error.response.status === 401 || error.response.data.message === 'Token expired')) {
        // JWT 令牌失效，跳转到登录页面
        router.push('/home');
        localStorage.removeItem('token'); // 替换为实际的令牌存储位置
        localStorage.removeItem('username');
        localStorage.removeItem('id');
        ElMessage.error('登录过期或未登录，功能受限，请重新登录');
        return Promise.reject(error);
    }
    return Promise.reject(error);
});

axios.defaults.timeout = 50000;


app.mount('#app');